//
//  ConnectModel.m
//  DouBanDemo
//
//  Created by LiuFei on 14/11/7.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "ConnectModel.h"
#import "Reachability.h"
#import "SaveFile.h"

@implementation ConnectModel

- (void)dealloc
{
    Block_release(_fblock);
    [_urlName release];
    [_receiveData release];
    [super dealloc];
}


// 网络判断
+ (BOOL)judgeConnectEnabled
{
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NetworkStatus status = [reach currentReachabilityStatus];
    switch (status) {
        case NotReachable:
        {
            return NO;
        }
            break;
        case ReachableViaWiFi:
        {
            return YES;
        }
            break;
        case ReachableViaWWAN:
        {
            return YES;
        }
            break;
        default:
            break;
    }
}

// GET
+ (void)getConnectGetWithUrl:(NSString *)urlStr parms:(NSMutableDictionary *)parms block:(FinishedBlock)block
{
    if ([ConnectModel judgeConnectEnabled]) {
        ConnectModel *model = [[ConnectModel alloc] init];
        model.fblock = block;
        [model startConnenctGet:urlStr dictionary:parms];
        [model release];
    } else {
        NSString *name = [ConnectModel getUrl:urlStr dictionary:parms];
        name = [name stringByReplacingOccurrencesOfString:@"/" withString:@""];
        // 读取本地数据
        NSData *data = [SaveFile readFileWithName:name];
        if (data) {

            block(data);
            
        }
    }
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.receiveData = [NSMutableData data];
    }
    return self;
}

+ (NSString *)getUrl:(NSString *)urlStr dictionary:(NSMutableDictionary *)dic
{
    NSString *str = @"";
    for (NSString *key in [dic allKeys]) {
        if ([str length] == 0) {
            str = [NSString stringWithFormat:@"?%@=%@", key, [dic objectForKey:key]];
        } else {
            str = [NSString stringWithFormat:@"%@&%@=%@", str, key, [dic objectForKey:key]];
        }
    }
    urlStr = [NSString stringWithFormat:@"%@%@", urlStr, str];
    return urlStr;
}

- (void)startConnenctGet:(NSString *)urlStr dictionary:(NSMutableDictionary *)dic
{
    urlStr = [ConnectModel getUrl:urlStr dictionary:dic];
    urlStr = [urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.urlName = [NSString stringWithFormat:@"%@", urlStr];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
    
    [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)startConnectPost:(NSString *)urlStr parms:(NSString *)parms
{
    urlStr = [urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPBody:[parms dataUsingEncoding:NSUTF8StringEncoding]];
    
    [NSURLConnection connectionWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_receiveData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // 获得文件名
    self.urlName = [self.urlName stringByReplacingOccurrencesOfString:@"/" withString:@""];
    // 写入本地
    [SaveFile writeFileWithName:self.urlName data:_receiveData];
    self.fblock(_receiveData);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"error:%@", error);
}



















@end
